package com.xjtu.platform.user.consultor.consultRecord;

import com.xjtu.platform.common.CommonService;
import com.xjtu.platform.common.Constants;
import com.xjtu.platform.common.Result;
import com.xjtu.platform.common.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

/**
 * @创建人 MBY381
 * @创建时间 2022/7/3 09:29
 * @描述 咨询记录管理
 */


@RestController
@RequestMapping("/consulter/consultRecord")
public class ConsultRecordAPI {


    @Resource
    private ConsultRecordService consultRecordService;
    @Resource
    private CommonService commonService;

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public Result getList(@RequestHeader String token,@RequestBody ConsultPageParam consultPageParam) {
        String user_id = TokenUtils.getUserInfo(token,commonService).getUserId();
        consultPageParam.setUser_id(user_id);
        try {
            Map<String, Object> page = consultRecordService.getListByConsultantId(consultPageParam);
            return Result.success("已查询到所有咨询记录",page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_BUSINESS,"系统错误！");
        }
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateState(@RequestBody ConsultRecordDto dto){
        Constants.checkPath();
        try {
            if(dto.getState()==4){
                consultRecordService.updateStateAndInfo(dto);
                consultRecordService.endUpConsult(dto);
                //待生成结案报告
                return Result.success("结案成功");
            }else {
                consultRecordService.updateStateAndInfo(dto);
                return Result.success("录入成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"系统错误");
    }


    @RequestMapping(value = "/getFile",method = RequestMethod.GET)
    public synchronized void getFile(@RequestParam String visit_id,@RequestParam String tag, HttpServletResponse response) throws IOException {
        String path;
        Constants.checkPath();
        if(tag.equals("true")){
            path =consultRecordService.makeXlsxOutFiles(visit_id);
        }else {
            path = consultRecordService.findPath(visit_id);
            if(path==null||visit_id==null){
                throw new RuntimeException("信息缺失");
            }
            System.out.println("id: "+visit_id+"收到一个查询请求，excel路径:"+path);
        }

        try {
            boolean exists=new File(path).exists();
            System.out.println("结案报告文件"+path+" "+exists);
            if(exists){
                InputStream in = new FileInputStream(path);
                OutputStream out = response.getOutputStream();
                byte[] b = new byte[10240];
                int len = -1;
                while((len=in.read(b))!=-1){
                    out.write(b,0,len);
                }
                out.flush();
                out.close();
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
